<?php

chdir(dirname(__FILE__)."/../..");
$path = getcwd();
chdir(dirname(__FILE__));

include_once $path."/../lib/core.class.php";


class DiscountsSnippet extends IPlugin 
{
    function DiscountsSnippet($parent = null)
    {
        IPlugin::IPlugin($parent);
        $this->tplfolder = dirname(__FILE__)."/tpl";
    }

    function getPluginTag() { return "discounts"; }
    function getPluginName() { return "Return discount"; }
    function getPluginVersion() { return "1.0.0"; }
    
    function Render($params = null)
    {

        $core = getCoreInstance();
        $framework = $core->getProperty("framework"); 
        $render = $framework->smarty;
        $db = $core->getProperty("db");

        $result = 0;
        return $result;
    }

/********************Verify Discount***********************************************************************/
/**********************************************************************************************************/
    
    function VerifyDiscount(&$db, $id_item, $code, $num, $price, $discount)
    {
        $sql = "select id_discount, title, code_coupon, group_items, date_begin, date_ending, amount_buying, type_amount_buying, type_form ";
        $sql .= " from discount";
        $sql .= " where id_discount = {$discount} and now() BETWEEN date_begin and date_ending";
//        echo $sql . "\n<br />\n";
        $data_discount = ArrayDb2Str($db->getAll($sql));

        $data_discount = $data_discount[0];
        switch ($data_discount['type_form'])
        {




            case "coupon" :
            { 
                if ($code == $data_discount['code_coupon'])
                {
                    $arr_items = split(",", $data_discount['group_items']);
                    if ((in_array($id_item, $arr_items)) || in_array(0, $arr_items))
                        return true;
                }

                break;
            }




            case "date" :
            { 
                $arr_items = split(",", $data_discount['group_items']);
                if (in_array($id_item, $arr_items) || in_array(0, $arr_items))
                    return true;

                break;
            }




            case "price/count discount" :
            { 
                if (($data_discount['type_amount_buying'] == "a" && $data_discount['amount_buying'] < $num) || 
                    ($data_discount['type_amount_buying'] == "$" && $data_discount['amount_buying'] < ($num * $price)))
                {
                    $arr_items = split(",", $data_discount['group_items']);
                    if (in_array($id_item, $arr_items) || in_array(0, $arr_items))
                        return true;
                }

                break;
            }


        }


        return false;

    }
    
/********************Verify Type Discount******************************************************************/
/**********************************************************************************************************/
    
    function VerifyTypeDiscount(&$db, $id_item, $code, $num, $price, $type)
    {
        $sql = "select id_discount, title";
        $sql .= " from discount";
        $sql .= " where id_type = {$type} and now() BETWEEN date_begin and date_ending order by title asc";
//        echo $sql . "\n<br />\n";
        $arr_discounts = ArrayDb2Str($db->getAll($sql));

        foreach ($arr_discounts as $discount)
        {
            if ($this->VerifyDiscount($db, $id_item, $code, $num, $price, $discount['id_discount']))
            {
                return $discount['id_discount'];
            }
        }
    }
    
/********************Get Id Discount***********************************************************************/
/**********************************************************************************************************/
    function  GetIdDiscount(&$db, $id_item, $code, $num, $price)
    {
        $arr_types = ArrayDb2Str($db->getAll("select id_type, title from discount_type order by sibling asc"));
        
        $id_discount = 0;
        foreach ($arr_types as $type)
        {
            if (($id_discount = $this->VerifyTypeDiscount($db, $id_item, $code, $num, $price, $type['id_type'])) != 0)
            {
                return $id_discount;
            }
        }
 
        return 0;
    }

/******************Get Value Discount***********************************************************************/
/**********************************************************************************************************/

    function GetValueDiscount($id_item, $code, $num, $price)
    {
        $core = &getCoreInstance();
        $db = $core->getProperty("db");

        // data about table with products
        $str_catalogid = $core->config['discounts']['catalogid'];
        $str_parent_id = $core->config['discounts']['parent_id'];
        $str_title = $core->config['discounts']['title'];
        $str_table = $core->config['discounts']['product_table'];
        $str_iscategory = $core->config['discounts']['iscategory'];
        $str_iscategory_true = $core->config['discounts']['iscategoryTrue'];
        $str_iscategory_false = $core->config['discounts']['iscategoryFalse'];
        $str_itemnumber = $core->config['discounts']['itemnumber'];

        $id_item_f = $id_item;
        while (0 == ($id_discount = $this->GetIdDiscount($db, $id_item_f, $code, $num, $price)))
        {
            if ($str_parent_id != "")
                $id_item_f = $db->getOne("select parent_id from {$str_table} where $str_catalogid = {$id_item_f}");
            else 
                $id_item_f = 0;
            
            if ($id_item_f == 0)
                return false;
        }



        $arr_id_discounts = array($id_discount);


        // Search discounts in group
        $sql = "select group_with";
        $sql .= " from discount_type, discount";
        $sql .= " where discount.id_discount = {$id_discount} and discount.id_type = discount_type.id_type";
        
        $group_with = $db->getOne($sql);

        $arr_discounts = ("" == $group_with|| "0" == $group_with) ? array('0') : split(",", $group_with);
        foreach($arr_discounts as $item)
        {
            if (strstr($item, "t"))
            {
                $type = substr($item, 1, strlen($item) - 1);
                $id_item_f = $id_item;
                while (0 == ($id_discount = $this->VerifyTypeDiscount(&$db, $id_item_f, $code, $num, $price, $type)))
                {
                    if ($str_parent_id != "")
                        $id_item_f = $db->getOne("select parent_id from {$str_table} where $str_catalogid = {$id_item_f}");
                    else 
                        $id_item_f = 0;
                    
                    if ($id_item_f == 0)
                        break;
                }
                if (0 != $id_discount)
                    $arr_id_discounts[] = $id_discount;
            }
            else if (strstr($item, "d"))
            {
                $id_discount = substr($item, 1, strlen($item) - 1);
                $id_item_f = $id_item;
                $flag = false;
                while (true != ($flag = $this->VerifyDiscount(&$db, $id_item_f, $code, $num, $price, $id_discount)))
                {
                    if ($str_parent_id != "")
                        $id_item_f = $db->getOne("select parent_id from {$str_table} where $str_catalogid = {$id_item_f}");
                    else 
                        $id_item_f = 0;
                    
                    if ($id_item_f == 0)
                        break;
                }
                if (true == $flag)
                    $arr_id_discounts[] = $id_discount;
            }
        }


        //print_r($arr_id_discounts);

        // Calc value discount
        
        $sql = "select value_discount, type_payment, type_form";
        $sql .= " from discount";
        $sql .= " where id_discount in (" . join(",", $arr_id_discounts) . ")";
        
        $arr_operating_discount = $db->getAll($sql);
        $res = 0;
        foreach($arr_operating_discount as $discount)
        {
            if ($discount['type_payment'] == "$")
                if (db2str($discount['type_form']) == "price/count discount")
                    $res += $discount['value_discount'];
                else
                    $res += $discount['value_discount'] * $num;
            else if ($discount['type_payment'] == "%")
            {
                $res +=  ($price * $num / 100) * $discount['value_discount'];
            }
        }
        
        return $res;
    }    
};


?>